<?php

namespace App\Http\Requests\Admin\Ca;

use App\Facade\AppContext;
use App\Http\Requests\BaseFormRequest;
use App\Models\Ca\Disease;

class CustomerSaveFormRequest extends BaseFormRequest
{
    protected function passedValidation()
    {
        $id = $this->get('id', 0);
        $yy_time = $this->get('yy_time', '');
        if (empty($yy_time)) {
            $this->offsetSet('yy_time', null);
        }

        $yy_time = $this->get('consultation_type', '');
        if (empty($yy_time)) {
            $this->offsetSet('consultation_type', 0);
        }

        $yy_time = $this->get('source_id', '');
        if (empty($yy_time)) {
            $this->offsetSet('source_id', 0);
        }

        $yy_time = $this->get('age', '');
        if (empty($yy_time)) {
            $this->offsetSet('age', 0);
        }
        $yy_time = $this->get('cost_money', '');
        if (empty($yy_time)) {
            $this->offsetSet('cost_money', 0);
        }

        $yy_time = $this->get('fb_time', '');
        if (empty($yy_time)) {
            $this->offsetSet('fb_time', null);
        }

        $hf_time = $this->get('hf_time', '');
        if (empty($hf_time)) {
            $this->offsetSet('hf_time', null);
        }
        $effective = $this->get('effective', '');
        if (empty($effective)) {
            $this->offsetSet('effective', 0);
        }


        $diseases = $this->get('diseases', []);
        if (!empty($diseases)) {
            $this->offsetSet('disease_data', Disease::query()->where('id', $diseases)->get(['id', 'name'])->toArray());
            $_diseases = [];
            foreach ($diseases as $diseaseId) {
                $_diseases[$diseaseId] = [
                    'disease_id' => $diseaseId
                ];
            }
            $this->offsetSet('diseases', $_diseases);
        } else {
            $this->offsetSet('disease_data', []);
            $this->offsetSet('diseases', []);
        }
        $this->offsetSet('real_name','');
        $this->offsetSet('qq','');
        $this->offsetSet('phone','');
        $this->offsetSet('address','');
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'name' => [
                'required',
                'between:2,50'
            ]
        ];
    }

    public function messages()
    {
        return [
            'name.required' => '请输入名称',
            'name.between' => '名称字符长度:min-:max'
        ];
    }
}